use "ZA6788_v2-0-0.dta", replace 
* ----------------------------------------------------------------------------
**# Data preparation
* ----------------------------------------------------------------------------
**********************
* treatment variable *
**********************
rename p1 date
fre date
gen tr=.
replace tr=0 if date<=6
replace tr=1 if date>7
label var tr "Trump victory"

******************************
* socio-demographic controls *
******************************
clonevar gender=d10
clonevar age=d11
clonevar occ=d15a_r2
clonevar urban=d25
clonevar marital=d7r1
clonevar household=d40t_r

fre d8
gen edu=. 
replace edu=d8
replace edu=0 if d8==97
replace edu=age if d8==98
replace edu=. if d8==.a
replace edu=. if d8==.b
replace edu=26 if d8>26 & d8!=97 
fre edu


******************************
* relabelling the covariates *
******************************
label var age "Age"
label var gender "Gender"
label var edu "Years of Schooling"
label var household "Household size"

label define lbocc 1"Self-employed" 2"Managers" 3"Other white collars"	4"Manual workers" ///
					5"House persons" 6"Unemployed"	7"Retired"	8"Students"
label values occ lbocc
label define lbmarital 1"(Re-)Married" 2"Single living with partner" 3"Single"	4"Divorced or separated" ///
					5"Widow" 6"Other"
label values marital lbmarital

*****************
* country of EU *
*****************
keep if  eu28==1 // SWD was only asked in EU28 countries
fre country
recode country (10=9) (14=4) // Combine Northern Ireland with Great Britain; Combine East Germany with West Germany 
fre country

label define lbctry 1"France " 2"Belgium" 3"The Netherlands" 4"Germany" 5"Italy" 6"Luxembourg" 7"Denmark" 8"Ireland" ///
					9"United Kingdom" 11"Greece" 12"Spain" 13"Portugal" 16"Finland" 17"Sweden" 18"Austria" 19"Cyprus" /// 
					20"Czech Republic" 21"Estonia" 22"Hungary" 23"Latvia" 24"Lithuania" 25"Malta" 26"Poland" 27"Slovakia" ///
					28"Slovenia" 29"Bulgaria" 30"Romania" 32"Croatia"
label values country lbctry
fre country

***********
* outcome *
***********
* satisfaction with democracy
fre qa18a 
clonevar swd=qa18a 
replace swd=. if swd==.a
recode swd (4=1) (3=2) (2=3) (1=4)
label define lbswd 1"Not at all satisfied" 2"Not very satisfied" 3"Fairly satisfied" 4"Very satisfied"
label values swd lbswd
tab swd qa18a, m
tab country swd , m // SWD was only asked in EU28
fre swd

* binary
gen swd_b=. 
replace swd_b=1 if swd==3 | swd==4 
replace swd_b=0 if swd==1 | swd==2 
replace swd_b=. if swd==.
tab swd swd_b, m

**************************************
* create entropy weight (by country) *
**************************************
sort country
gen w_main=.
foreach x in "1" "2" "3" "4" "5" "6" "7" "8" "9" "11" "12" "13" "16" "17" "18" "19" "20" "21" "22" "23" "24" "25" "26" "27" "28" "29" "30" "32"{
	ebalance tr c.age i.gender c.edu i.urban i.occ i.marital c.household if country==`x', tar(2) 
	replace w_main=_webal if country==`x'
}
drop _webal

******************************
* create dummy for each NUTS *
******************************
encode nuts, generate(d_nuts) 
tab d_nuts tr, nol

forvalues i=1/246 {
    quietly levelsof tr if d_nuts==`i'
    if r(r)!=2 {
        di "NUTS == `i' does not contain two levels "
    }
}

* 16 out of 246 NUTS are imblanced


****************
* online media *
****************
clonevar internet = qe3_5r
clonevar online_net = qe3_6r
fre internet online_net

*********************
* append V-Dem data *
*********************
gen country_id=. // this is to match the the country_id from V-Dem data
replace country_id=5 if country==17 // "Sweden"
replace country_id=17 if country==26 // "Poland"
replace country_id=21 if country==13 // "Portugal"
replace country_id=76 if country==1 // "France"
replace country_id=77 if country==4 // "Germany"
replace country_id=81 if country==8 // "Ireland"
replace country_id=82 if country==5 // "Italy"
replace country_id=84 if country==23 // "Latvia"
replace country_id=91 if country==3 // "The Netherlands"
replace country_id=96 if country==12 // "Spain"
replace country_id=101 if country==9 // "United Kingdom"
replace country_id=144 if country==18 // "Austria"
replace country_id=148 if country==2 // "Belgium"
replace country_id=152 if country==29 // "Bulgaria"
replace country_id=154 if country==32 // "Croatia"
replace country_id=156 if country==19 // "Cyprus"
replace country_id=157 if country==20 // "Czech Republic"
replace country_id=158 if country==7 // "Denmark"
replace country_id=161 if country==21 // "Estonia"
replace country_id=163 if country==16 // "Finland"
replace country_id=164 if country==11 // "Greece"
replace country_id=173 if country==24 // "Lithuania"
replace country_id=174 if country==6 // "Luxembourg"
replace country_id=178 if country==25 // "Malta"
replace country_id=190 if country==30 // "Romania"
replace country_id=201 if country==27 // "Slovakia"
replace country_id=202 if country==28 // "Slovenia"
replace country_id=210 if country==22 // "Hungary"

merge m:1 country_id using "vdem_trimmed.dta"

* Dummy for old democracies 
tab country v2regdur, m
gen age_dem=0 if v2regdur<10000
replace age_dem=1 if v2regdur>10000
tab country age_dem

save "eb.dta", replace

* ----------------------------------------------------------------------------
**# Covariates balance tests (by country)
* ----------------------------------------------------------------------------
use "eb.dta", replace
set scheme white_ptol

* table B.2: age gender edu household (Sheet 1)
* table B.3: urban1 urban2 urban3 (Sheet 2)
* table B.4: occ1 occ2 occ3 occ4 occ5 occ6 occ7 occ8 (Sheet 3)
* table B.5: marital1 marital2 marital3 marital4 marital5 marital6 (Sheet 4)

local countries 1 2 3 4 5 6 7 8 9 11 12 13 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 32
local ctry_names France Belgium Netherlands Germany Italy Luxembourg Denmark Ireland UK Greece Spain Portugal Finland Sweden Austria Cyprus "Czech Republic" Estonia Hungary Latvia Lithuania Malta Poland Slovakia Slovenia Bulgaria Romania Croatia

matrix bt1 = J(28,8,.)
matrix rownames bt1 = `ctry_names'
matrix colnames bt1 = age p-value gender p-value edu p-value household p-value

local r=1
foreach j in `countries' {
	local c=1
	foreach var in age gender edu household {
		reg tr `var' if country==`j'
		matrix bt1[`r',`c++'] = e(b)[1,1]
		matrix bt1[`r',`c++'] = 2*ttail(e(df_r),abs(_b[`var']/_se[`var']))
	}
	local r = `r'+1
}

matrix bt2 = J(28,6,.)
matrix rownames bt2 = `ctry_names'
matrix colnames bt2 = urban1 p-value urban2 p-value urban3 p-value

local r=1
foreach j in `countries' {
	local c=1
	reg tr i.urban if country==`j'
	matrix bt2[`r',`c++'] = e(b)[1,1]
	matrix bt2[`r',`c++'] = 2*ttail(e(df_r),abs(_b[1.urban]/_se[1.urban]))
	matrix bt2[`r',`c++'] = e(b)[1,2]
	matrix bt2[`r',`c++'] = 2*ttail(e(df_r),abs(_b[2.urban]/_se[2.urban]))
	matrix bt2[`r',`c++'] = e(b)[1,3]
	matrix bt2[`r',`c++'] = 2*ttail(e(df_r),abs(_b[3.urban]/_se[3.urban]))
	local r = `r'+1
}
matrix list bt2

matrix bt3 = J(28,16,.)
matrix rownames bt3 = `ctry_names'
matrix colnames bt3 = occ1 p-value occ2 p-value occ3 p-value occ4 p-value occ5 p-value occ6 p-value occ7 p-value occ8 p-value

local r=1
foreach j in `countries' {
	local c=1
	reg tr i.occ if country==`j'
	matrix bt3[`r',`c++'] = e(b)[1,1]
	matrix bt3[`r',`c++'] = 2*ttail(e(df_r),abs(_b[1.occ]/_se[1.occ]))
	matrix bt3[`r',`c++'] = e(b)[1,2]
	matrix bt3[`r',`c++'] = 2*ttail(e(df_r),abs(_b[2.occ]/_se[2.occ]))
	matrix bt3[`r',`c++'] = e(b)[1,3]
	matrix bt3[`r',`c++'] = 2*ttail(e(df_r),abs(_b[3.occ]/_se[3.occ]))
	matrix bt3[`r',`c++'] = e(b)[1,4]
	matrix bt3[`r',`c++'] = 2*ttail(e(df_r),abs(_b[4.occ]/_se[4.occ]))
	matrix bt3[`r',`c++'] = e(b)[1,5]
	matrix bt3[`r',`c++'] = 2*ttail(e(df_r),abs(_b[5.occ]/_se[5.occ]))
	matrix bt3[`r',`c++'] = e(b)[1,6]
	matrix bt3[`r',`c++'] = 2*ttail(e(df_r),abs(_b[6.occ]/_se[6.occ]))
	matrix bt3[`r',`c++'] = e(b)[1,7]
	matrix bt3[`r',`c++'] = 2*ttail(e(df_r),abs(_b[7.occ]/_se[7.occ]))
	matrix bt3[`r',`c++'] = e(b)[1,8]
	matrix bt3[`r',`c++'] = 2*ttail(e(df_r),abs(_b[8.occ]/_se[8.occ]))
	local r = `r'+1
}
matrix list bt3

matrix bt4 = J(28,12,.)
matrix rownames bt4 = `ctry_names'
matrix colnames bt4 = marital1 p-value marital2 p-value marital3 p-value marital4 p-value marital5 p-value marital6 p-value

local r=1
foreach j in `countries' {
	local c=1
	reg tr i.marital if country==`j'
	matrix bt4[`r',`c++'] = e(b)[1,1]
	matrix bt4[`r',`c++'] = 2*ttail(e(df_r),abs(_b[1.marital]/_se[1.marital]))
	matrix bt4[`r',`c++'] = e(b)[1,2]
	matrix bt4[`r',`c++'] = 2*ttail(e(df_r),abs(_b[2.marital]/_se[2.marital]))
	matrix bt4[`r',`c++'] = e(b)[1,3]
	matrix bt4[`r',`c++'] = 2*ttail(e(df_r),abs(_b[3.marital]/_se[3.marital]))
	matrix bt4[`r',`c++'] = e(b)[1,4]
	matrix bt4[`r',`c++'] = 2*ttail(e(df_r),abs(_b[4.marital]/_se[4.marital]))
	matrix bt4[`r',`c++'] = e(b)[1,5]
	matrix bt4[`r',`c++'] = 2*ttail(e(df_r),abs(_b[5.marital]/_se[5.marital]))
	capture {
		matrix bt4[`r',`c++'] = e(b)[1,`=colnumb(e(b),"6.marital")']
		matrix bt4[`r',`c++'] = 2*ttail(e(df_r),abs(_b[6.marital]/_se[6.marital]))
	}
	local r = `r'+1
}
matrix list bt4

putexcel set "balance tests.xls", sheet("Sheet 1", replace) replace 
putexcel A1 = matrix(bt1), names
putexcel set "balance tests.xls", sheet("Sheet 2", replace) modify
putexcel A1 = matrix(bt2), names
putexcel set "balance tests.xls", sheet("Sheet 3", replace) modify
putexcel A1 = matrix(bt3), names
putexcel set "balance tests.xls", sheet("Sheet 4", replace) modify
putexcel A1 = matrix(bt4), names
putexcel clear


* ----------------------------------------------------------------------------
**# Main Effect 
* ----------------------------------------------------------------------------
use "eb.dta", replace
set scheme white_ptol

* define control variables
global controls "age c.age#c.age i.gender c.edu c.household i.urban i.occ i.marital"

* effective sample
tab tr, m

*******
* OLS *
*******
eststo main_c: reg swd i.tr i.d_nuts $controls if date!=7, vce(cluster nuts)  
quietly estadd local ctrl "Yes": main_c
quietly estadd local cfe "Yes": main_c
quietly estadd local clust `e(N_clust)': main_c

* compare to educ
display (.0272204/.0106861)

************
* ebalance *
************
eststo e_main: reg swd i.tr i.d_nuts if date!=7 [pweight=w_main], vce(cluster nuts)
quietly estadd local ctrl "No": e_main
quietly estadd local cfe "Yes": e_main

********************************
* Recode SWD into binary (OLS) *
********************************
eststo b_main: reg swd_b i.tr i.d_nuts $controls if date!=7, vce(cluster nuts)
quietly estadd local ctrl "Yes": b_main
quietly estadd local cfe "Yes": b_main

**********************************
* Recode SWD into binary (Logit) *
**********************************
eststo logit_main: logit swd_b i.tr i.d_nuts $controls if date!=7, vce(cluster nuts)
quietly estadd local ctrl "Yes": logit_main
quietly estadd local cfe "Yes": logit_main

**********
* ologit *
**********
eststo o_main: ologit swd i.tr i.d_nuts $controls if date!=7, vce(cluster nuts)
quietly estadd local ctrl "Yes":o_main
quietly estadd local cfe "Yes": o_main


* regression table (in main text)
esttab  main_c e_main b_main logit_main o_main using "eb_main.rtf", b(2) se(2) replace /// 
	scalars("cfe NUTS FE" "ctrl Controls" "N Observations") 	///
	label wide note onecell noomitted nonumbers lines keep (1.tr) ///
	coeflabel(1.tr "Trump victory") varwidth(12) ///
	mtitles("(1) OLS (4-point scale)" "(2) Entropy Balancing" "(3) OLS (dichotomize SWD)" "(4) Logit (dichotomize SWD)" "(5) Ordered Logit")  modelwidth(8) ///
	star(+ 0.10 * 0.05 ** 0.01 *** 0.001)  ///
	note("Note: Entries are coefficients; standard errors clustered at the NUTS level are shown in parentheses. Models 1, 3-5 include socio-demographic controls (age, square term of age, gender, years of education, place of residence, occupation, marital status, and household size). Model 2 is entropy-balanced using the aforementioned socio-demographic variables; the highest order of moment constraints is set at the variance for each covariate when creating the entropy weight.") ///
	title("Table 1. The spillover effect of Trump's electoral victory on citizens' SWD") 
	
* regression table (full with covariates)
esttab  main_c e_main b_main logit_main o_main using "eb_main_full.rtf", b(2) se(2) replace /// 
	scalars("cfe NUTS FE" "ctrl Controls" "N Observations") 	///
	keep (1.tr age c.age#c.age 2.gender edu household 2.urban 3.urban 2.occ 3.occ 4.occ 5.occ 6.occ 7.occ 8.occ 2.marital 3.marital 4.marital 5.marital 6.marital /:cut1 /:cut2 /:cut3) ///
	label wide note onecell noomitted nonumbers lines  ///
	coeflabel(1.tr "Trump victory") varwidth(18) ///
	mtitles("(1) OLS (4-point scale)" "(2) Entropy Balancing" "(3) OLS (dichotomize SWD)" "(4) Logit (dichotomize SWD)" "(5) Ordered Logit")  modelwidth(7) ///
	star(+ 0.10 * 0.05 ** 0.01 *** 0.001)  ///
	note("Note: Entries are coefficients; standard errors clustered at the NUTS level are shown in parentheses. Models 1, 3-5 include socio-demographic controls (age, square term of age, gender, years of education, place of residence, occupation, marital status, and household size). Model 2 is entropy-balanced using the aforementioned socio-demographic variables; the highest order of moment constraints is set at the variance for each covariate when creating the entropy weight.") ///
	title("Table B.6. The spillover effect of Trump's electoral victory on citizens' SWD") 	

* ----------------------------------------------------------------------------
**# Robustness checks
* ----------------------------------------------------------------------------
********************
* Bandwidth: 1 day *
********************
eststo main_bw_1: reg swd i.tr i.d_nuts $controls if date==6 | date==8, vce(cluster nuts)  
quietly estadd local ctrl "Yes": main_bw_1
quietly estadd local cfe "Yes": main_bw_1

eststo e_bw_1: reg swd i.tr i.d_nuts if date==6 | date==8 [pweight=w_main], vce(cluster nuts)
quietly estadd local ctrl "No": e_bw_1
quietly estadd local cfe "Yes": e_bw_1

eststo b_bw_1: reg swd_b i.tr i.d_nuts $controls if date==6 | date==8, vce(cluster nuts)
quietly estadd local ctrl "Yes": b_bw_1
quietly estadd local cfe "Yes": b_bw_1

eststo logit_bw_1: logit swd_b i.tr i.d_nuts $controls if date==6 | date==8, vce(cluster nuts)
quietly estadd local ctrl "Yes": logit_bw_1
quietly estadd local cfe "Yes": logit_bw_1

eststo o_bw_1: ologit swd i.tr i.d_nuts $controls if date==6 | date==8, vce(cluster nuts)
quietly estadd local ctrl "Yes":o_bw_1
quietly estadd local cfe "Yes": o_bw_1


* regression table (full with covariates)
esttab  main_bw_1 e_bw_1 b_bw_1 logit_bw_1 o_bw_1 using "bw_1_full.rtf.rtf", b(2) se(2) replace /// 
	scalars("cfe NUTS FE" "ctrl Controls" "N Observations") 	///
	keep (1.tr age c.age#c.age 2.gender edu household 2.urban 3.urban 2.occ 3.occ 4.occ 5.occ 6.occ 7.occ 8.occ 2.marital 3.marital 4.marital 5.marital 6.marital /:cut1 /:cut2 /:cut3) ///
	label wide note onecell noomitted nonumbers lines  ///
	coeflabel(1.tr "Trump victory") varwidth(18) ///
	mtitles("(1) OLS (4-point scale)" "(2) Entropy Balancing" "(3) OLS (dichotomize SWD)" "(4) Logit (dichotomize SWD)" "(5) Ordered Logit")  modelwidth(7) ///
	star(+ 0.10 * 0.05 ** 0.01 *** 0.001)  ///
	note("Note: Entries are coefficients; standard errors clustered at the NUTS level are shown in parentheses. Models 1, 3-5 include socio-demographic controls (age, square term of age, gender, years of education, place of residence, occupation, marital status, and household size). Model 2 is entropy-balanced using the aforementioned socio-demographic variables; the highest order of moment constraints is set at the variance for each covariate when creating the entropy weight.") ///
	title("Table B.7. Bandwidth restriction to 1 Day before and after Trump's victory") 

********************
* Permutation test *
********************
permute tr _b[1.tr], strata(country) rep(5000): reg swd i.tr i.d_nuts $controls if date!=7, vce(cluster nuts) // stratify the permutation by country

**************
* Country FE *
**************
eststo main_c_ctry: reg swd i.tr i.country $controls if date!=7, vce(cluster nuts) 
quietly estadd local ctrl "Yes": main_c_ctry
quietly estadd local cfe "Yes": main_c_ctry
quietly estadd local clust `e(N_clust)': main_c_ctry

* ebalance
eststo e_main_ctry: reg swd i.tr i.country if date!=7 [pweight=w_main], vce(cluster nuts)
quietly estadd local ctrl "No": e_main_ctry
quietly estadd local cfe "Yes": e_main_ctry

* Recode SWD into binary (OLS) 
eststo b_main_ctry: reg swd_b i.tr i.country $controls if date!=7, vce(cluster nuts)
quietly estadd local ctrl "Yes": b_main_ctry
quietly estadd local cfe "Yes": b_main_ctry
quietly estadd local clust `e(N_clust)': b_main_ctry

* Recode SWD into binary (Logit) 
eststo logit_main_ctry: logit swd_b i.tr i.country $controls if date!=7, vce(cluster nuts)
quietly estadd local ctrl "Yes": logit_main_ctry
quietly estadd local cfe "Yes": logit_main_ctry
quietly estadd local clust `e(N_clust)': logit_main_ctry

* ologit
eststo o_main_ctry: ologit swd i.tr i.country $controls if date!=7, vce(cluster nuts)
quietly estadd local ctrl "Yes":o_main_ctry
quietly estadd local cfe "Yes": o_main_ctry
quietly estadd local clust `e(N_clust)': o_main_ctry

* regression table (full with covariates)
esttab  main_c_ctry e_main_ctry b_main_ctry logit_main_ctry o_main_ctry using "ctry_fe_full.rtf", b(2) se(2) replace /// 
	scalars("cfe Country FE" "ctrl Controls" "N Observations") 	///
	label wide note onecell noomitted nonumbers lines  ///
	keep (1.tr age c.age#c.age 2.gender edu household 2.urban 3.urban 2.occ 3.occ 4.occ 5.occ 6.occ 7.occ 8.occ 2.marital 3.marital 4.marital 5.marital 6.marital /:cut1 /:cut2 /:cut3) ///
	coeflabel(1.tr "Trump victory") varwidth(18) ///
	mtitles("(1) OLS (4-point scale)" "(2) Entropy Balancing" "(3) OLS (dichotomize SWD)" "(4) Logit (dichotomize SWD)" "(5) Ordered Logit")  modelwidth(8) ///
	star(+ 0.10 * 0.05 ** 0.01 *** 0.001)  ///
	note("Note: Entries are coefficients; standard errors clustered at the NUTS level are shown in parentheses. Models 1, 3-5 include socio-demographic controls (age, square term of age, gender, years of education, place of residence, occupation, marital status, and household size). Model 2 is entropy-balanced using the aforementioned socio-demographic variables; the highest order of moment constraints is set at the variance for each covariate when creating the entropy weight.") ///
	title("Table B.9. Using country fixed effects") 
	
* ----------------------------------------------------------------------------
**# Heterogenous effect
* ----------------------------------------------------------------------------
* media consumption (individual level)
reg swd i.tr##i.internet i.d_nuts $controls if date!=7, vce(cluster nuts)  
eststo internet_c: margins, dydx(tr) post level(95) at(internet=(1(1)5))
quietly estadd local ctrl "Yes": internet_c
quietly estadd local cfe "Yes": internet_c


reg swd i.tr##i.online_net i.d_nuts $controls if date!=7, vce(cluster nuts)  
eststo online_net_c: margins, dydx(tr) post level(95) at(online_net=(1(1)5)) 
quietly estadd local ctrl "Yes": online_net_c
quietly estadd local cfe "Yes": online_net_c

esttab internet_c online_net_c using "online_dydx.rtf", b(2) se(2) replace ///
	scalars("cfe NUTS FE" "ctrl Controls" "N Observations")  ///
	coeflabel(1._at "(Almost) every day" 2._at "1-3 times a week" 3._at "2 or 3 times a month/Less often" 4._at "Never" 5._at "No access") varwidth(18) ///	
	mtitles("Internet" "Online social networks")  modelwidth(10) ///
	star(+ 0.1 * 0.05 ** 0.01 *** 0.001)  ///
	note("Note: Entries are marginal effects of the treatment using OLS. DV: 4-point scale of SWD. Standard errors are shown in parentheses and are clustered at the NUTS level. The models include demographic controls (age, square term of age, gender, years of education, place of residence, occupation, marital status, and household size).") ///
	title("Table B.11. Marginal effect of Trump's electoral victory across citizens with different usage of the Internet and online social networks") 
 
 
* country-level (V-dem)
reg swd i.tr##i.age_dem $controls if date!=7, vce(cluster nuts)  // 
eststo age_dem_main: margins, dydx(tr) post level(95) at(age_dem=(0 1))
quietly estadd local ctrl "Yes": age_dem_main
quietly estadd local cfe "No": age_dem_main

reg swd i.tr##i.age_dem if date!=7 [pweight=w_main], vce(cluster nuts)
eststo age_dem_e_main: margins, dydx(tr) post level(95) at(age_dem=(0 1))
quietly estadd local ctrl "No": age_dem_e_main
quietly estadd local cfe "No": age_dem_e_main

reg swd_b i.tr##i.age_dem $controls if date!=7, vce(cluster nuts)
eststo age_dem_b_main: margins, dydx(tr) post level(95) at(age_dem=(0 1))
quietly estadd local ctrl "Yes": age_dem_b_main
quietly estadd local cfe "No": age_dem_b_main

logit swd_b i.tr##i.age_dem $controls if date!=7, vce(cluster nuts)
eststo age_dem_logit_main: margins, dydx(tr) post level(95) at(age_dem=(0 1))
quietly estadd local ctrl "Yes": age_dem_logit_main
quietly estadd local cfe "No": age_dem_logit_main


esttab age_dem_main age_dem_e_main age_dem_b_main age_dem_logit_main using "age_dem_dydx.rtf", b(2) se(2) replace ///
	scalars("cfe NUTS FE" "ctrl Controls" "N Observations")  ///
	coeflabel(1._at "New Democracies" 2._at  "Old Democracies") varwidth(18) ///	
	mtitles("OLS (4-point scale)" "Entropy Balancing" "OLS (dichotomize SWD)" "Logit (dichotomize SWD)")  modelwidth(8) ///
	star(+ 0.10 * 0.05 ** 0.01 *** 0.001)  ///
	note("Note: Entries are marginal effects of the treatment. DV: 4-point scale of SWD. Standard errors are shown in parentheses and are clustered at the NUTS level. Models 1, 3 and 4 include socio-demographic controls (age, square term of age, gender, level of education, children in household, ethnic minority status, type of community).") ///
	title("Table B.12. Marginal effect of Trump's electoral victory across new and old democracies") 	 
 

ologit swd i.tr##i.age_dem $controls if date!=7, vce(cluster nuts)
eststo age_dem_o_main: margins, dydx(tr) post level(95) at(age_dem=(0 1))
quietly estadd local ctrl "Yes": age_dem_o_main
quietly estadd local cfe "No": age_dem_o_main

* The table in the appendix is re-arranged to aid reading
esttab age_dem_o_main using "age_dem_dydx_ologit.rtf", b(2) se(2) replace ///
	scalars("cfe NUTS FE" "ctrl Controls" "N Observations")  ///
	coeflabel(1._predict#1._at "New Democracies (Point 1)" 1._predict#2._at  "Old Democracies (Point 1)" ///
	2._predict#1._at "New Democracies (Point 2)" 2._predict#2._at  "Old Democracies (Point 2)" ///
	3._predict#1._at "New Democracies (Point 3)" 3._predict#2._at  "Old Democracies (Point 3)" ///
	4._predict#1._at "New Democracies (Point 4)" 4._predict#2._at  "Old Democracies (Point 4)") varwidth(18) ///	
	mtitles(" ")  modelwidth(8) ///
	star(+ 0.10 * 0.05 ** 0.01 *** 0.001)  ///
	note("Note: Entries are marginal effects of the treatment. DV: 4-point scale of SWD. Standard errors are shown in parentheses and are clustered at the NUTS level. The model includes socio-demographic controls (age, square term of age, gender, level of education, children in household, ethnic minority status, type of community).") ///
	title("Table B.13. Marginal effect of Trump's electoral victory across new and old democracies (ordered logit)") 	


* ----------------------------------------------------------------------------
**# Placebo using 2012 
* ----------------------------------------------------------------------------
use "ZA5685_v2-0-0.dta", replace 
rename p1 date
fre date
gen tr=.
replace tr=0 if date<=6
replace tr=1 if date>7

********************************
* socio-demographic covariates *
********************************
clonevar gender=d10
clonevar age=d11
clonevar occ=d15a_r2
clonevar urban=d25
clonevar marital=d7r1
clonevar household=d40t_r

fre d8
gen edu=. 
replace edu=d8
replace edu=0 if d8==97
replace edu=age if d8==98
replace edu=. if d8==.a
replace edu=. if d8==.b
replace edu=26 if d8>26 & d8!=97 
fre edu

******************************
* relabelling the covariates *
******************************
fre age gender edu urban occ marital household
label var age "Age"
label var gender "Gender"
label var edu "Years of Schooling"
label var household "Household size"
label define lbocc 1"Self-employed" 2"Managers" 3"Other white collars"	4"Manual workers" ///
 					5"House persons" 6"Unemployed"	7"Retired"	8"Students"
label values occ lbocc
label define lbmarital 1"(Re-)Married" 2"Single living with partner" 3"Single"	4"Divorced or separated" ///
					5"Widow" 6"Other"
label values marital lbmarital

***********
* country *
***********
keep if  eu28==1 // SWD was only asked in EU countries back then 
fre country
recode country (10=9) (14=4) // Combine NIR Northern Ireland with GBN Great Britain; Combine DE-E Germany East with  Germany - West
fre country

label define lbctry 1"France " 2"Belgium" 3"The Netherlands" 4"Germany" 5"Italy" 6"Luxembourg" 7"Denmark" 8"Ireland" ///
					9"United Kingdom" 11"Greece" 12"Spain" 13"Portugal" 16"Finland" 17"Sweden" 18"Austria" 19"Cyprus" /// 
					20"Czech Republic" 21"Estonia" 22"Hungary" 23"Latvia" 24"Lithuania" 25"Malta" 26"Poland" 27"Slovakia" ///
					28"Slovenia" 29"Bulgaria" 30"Romania" 32"Croatia"
label values country lbctry


***********
* outcome *
***********
*  satisfaction with democracy
fre qa19a 
clonevar swd=qa19a 
replace swd=. if swd==.d
replace swd=. if swd==.i
recode swd (4=1) (3=2) (2=3) (1=4)
label define lbswd 1"Not at all satisfied" 2"Not very satisfied" 3"Fairly satisfied" 4"Very satisfied"
label values swd lbswd
tab swd qa19a, m
tab country swd , m // structural missing for Croatia, as it was not yet a EU member state in 2012


* binary
gen swd_b=. 
replace swd_b=1 if swd==3 | swd==4 
replace swd_b=0 if swd==1 | swd==2 
replace swd_b=. if swd==.

*************************
* create entropy weight *
*************************
sort country
gen w_main=.
foreach x in "1" "2" "3" "4" "5" "6" "7" "8" "9" "11" "12" "13" "16" "17" "18" "19" "20" "21" "22" "23" "24" "25" "26" "27" "28" "29" "30" "32"{
	ebalance tr c.age i.gender c.edu i.urban i.occ i.marital c.household if country==`x', tar(2) 
	replace w_main=_webal if country==`x'
}
drop _webal

******************************
* create dummy for each NUTS *
******************************
encode nuts, generate(d_nuts)

*********************
* main effect (OLS) *
*********************
* define control variables
global controls "age c.age#c.age i.gender c.edu c.household i.urban i.occ i.marital"

eststo p_main_c: reg swd i.tr i.d_nuts $controls if date!=7, vce(cluster nuts)  
quietly estadd local ctrl "Yes": p_main_c
quietly estadd local cfe "Yes": p_main_c
quietly estadd local clust `e(N_clust)': p_main_c

************************************************************************
* ebalance: option vce() of regress is not allowed with the svy prefix *
************************************************************************
eststo p_e_main: reg swd i.tr i.d_nuts if date!=7 [pweight= w_main], vce(cluster nuts)
quietly estadd local ctrl "No": p_e_main
quietly estadd local cfe "Yes": p_e_main

********************************
* Recode SWD into binary (OLS) *
********************************
eststo p_b_main: reg swd_b i.tr i.d_nuts $controls if date!=7, vce(cluster nuts)
quietly estadd local ctrl "Yes": p_b_main
quietly estadd local cfe "Yes": p_b_main
quietly estadd local clust `e(N_clust)': p_b_main


**********************************
* Recode SWD into binary (Logit) *
**********************************
eststo p_logit_main: logit swd_b i.tr i.d_nuts $controls if date!=7, vce(cluster nuts)
quietly estadd local ctrl "Yes": p_logit_main
quietly estadd local cfe "Yes": p_logit_main
quietly estadd local clust `e(N_clust)': p_logit_main

**********
* ologit *
**********
eststo p_o_main: ologit swd i.tr i.d_nuts $controls if date!=7, vce(cluster nuts)
quietly estadd local ctrl "Yes":p_o_main
quietly estadd local cfe "Yes": p_o_main
quietly estadd local clust `e(N_clust)': p_o_main

* regression table (full with covariates)
esttab  p_main_c p_e_main p_b_main p_logit_main p_o_main using "placebo_full.rtf", b(2) se(2) replace /// 
	scalars("cfe NUTS FE" "ctrl Controls" "N Observations") 	///
	label wide note onecell noomitted nonumbers lines  ///
	keep (1.tr age c.age#c.age 2.gender edu household 2.urban 3.urban 2.occ 3.occ 4.occ 5.occ 6.occ 7.occ 8.occ 2.marital 3.marital 4.marital 5.marital 6.marital /:cut1 /:cut2 /:cut3) ///
	coeflabel(1.tr "Obama victory") varwidth(18) ///
	mtitles("(1) OLS (4-point scale)" "(2) Entropy Balancing" "(3) OLS (dichotomize SWD)" "(4) Logit (dichotomize SWD)" "(5) Ordered Logit")  modelwidth(8) ///
	star(+ 0.10 * 0.05 ** 0.01 *** 0.001)  ///
	note("Note: Entries are coefficients; standard errors clustered at the NUTS level are shown in parentheses. Models 1, 3-5 include socio-demographic controls (age, square term of age, gender, years of education, place of residence, occupation, marital status, and household size). Model 2 is entropy-balanced using the aforementioned socio-demographic variables; the highest order of moment constraints is set at 2 (variance) for each covariate when creating the entropy weight.") ///
	title("Table B.10. Placebo tests using the 2012 Eurobarometer") 